Hi
I am experimenting to run my own daemon on start up.
I added this line to my /etc/inittab
oc:5:respawn:/home/fred/bin/my_daemon
here my daemon calls a C++ program
****************************************************************
$ cat /home/fred/bin/my_daemon.sh
#!/bin/sh
./proj
here what the C++ program does
****************************************************************
here what it does
~:$ ./proj
man ...................
and it is locate in my home dir.
is this all fine?
thanks
I am experimenting to run my own daemon on start up.
I added this line to my /etc/inittab
oc:5:respawn:/home/fred/bin/my_daemon
I am experimenting to run my own daemon on start up.
I added this line to my /etc/inittab
oc:5:respawn:/home/fred/bin/my_daemon
/home/fred/bin/proj
(remember to add a '&' to the end of the line above, if the C++
program is not yet a real daemon).
here what the C++ program does
****************************************************************
here what it does
~:$ ./proj
man ...................
and it is locate in my home dir.
is this all fine?
| I am experimenting to run my own daemon on start up.
| I added this line to my /etc/inittab
| oc:5:respawn:/home/fred/bin/my_daemon
Like told before, you should put a file in the /etc/init.d directory
and name it properly, like "S66mydaemon". Better would be to create a
link of this name, which would point to the script.
Why not use the /etc/rc.local file with execution in background, like:
[/etc/rc.local:]
...
...
/home/fred/bin/my_daemon.sh &
[end of file /etc/rc.local]
or even use directly:
/home/fred/bin/proj
(remember to add a '&' to the end of the line above, if the C++
program is not yet a real daemon).
the whole concept is very new to me, so I am learning and trying to
get the bigger picture.
what do you mean by "if the C++ program is not yet a real daemon"?
if I add
/home/fred/bin/proj &
to the end of /etc/rc.local, how does this re-establish my connection
with the server?
I mean, is there a return value from proj to let init know to restart
it?
what proj does is;
first connect to the server using uername/password.
send/receive info to/from the server.
if I use isLogged() in my C++ to return a false to init so it restarts
it, is not that the same as getting the C++ code to reconnect with out
needing a daemon?
| I am experimenting to run my own daemon on start up.
| I added this line to my /etc/inittab
| oc:5:respawn:/home/fred/bin/my_daemon
| Like told before, you should put a file in the /etc/init.d directory
| and name it properly, like "S66mydaemon". Better would be to create a
| link of this name, which would point to the script.
| Why not use the /etc/rc.local file with execution in background, like:
| [/etc/rc.local:]
| ...
| ...
| /home/fred/bin/my_daemon.sh &
| [end of file /etc/rc.local]
| or even use directly:
| /home/fred/bin/proj
| (remember to add a '&' to the end of the line above, if the C++
| program is not yet a real daemon).
thanks Bogdan;
the whole concept is very new to me, so I am learning and trying to
get the bigger picture.
what do you mean by "if the C++ program is not yet a real daemon"?
- put the program in daemon mode, using the daemon() function, unless
the program must be interactive (like it requires constant data input
from the keyboard). If the program is interactive, use the '&' at the
edn of the line (so it won't stop init and system startup from going
on) and don't use the daemon() function.
- connect
- talk to server
- repeatedly check if the program is connected using the isLogged()
function, perhaps from a new thread (pthread_create() or fork() ).
- if it isn't connected, start the connection.
However, if the program is interactive, I still can't imagine this to
work, because there may be no terminals yet to read data from. To be
more helpful, I need more details (if there are more questions).
Perhaps putting the program startup command in the shell startup
scripts would be more useful (without the '&' ,for example).
| | I am experimenting to run my own daemon on start up.
...
However, if the program is interactive, I still can't imagine this to
work, because there may be no terminals yet to read data from. To be
more helpful, I need more details (if there are more questions).
Perhaps putting the program startup command in the shell startup
scripts would be more useful (without the '&' ,for example).
#include "xyzCompany.h"
...
using namespace xyzCompany;
using namespace std;
int main()
{
xyzType myObj;
char* username=gary;
char* password=garyw;
while(1)
{
try {
myObj.login(username, password);
}
catch(myException f)
{
cout << "sample1: unable to connect, retying...." << endl;
continue;
}
break;
}
// wait for command line input
string c;
while( cin | c){ // cin is a standard input xterm prompt in
this case
if (c==end) myObj.logout();
if (c==dotask1) myObj.task1();
if (c==dotask2) myObj.task2();
...
}
say I lose power, the box reboots, I have auto-login-on, and a
line "xterm &" in my .bash_profile to start xterm on x login, and
"path/to/proj" in .bashrc to start myProject when ever a xterm
starts (not good because it will start a new instance every time I
open a new xterm which is not what I want, once only is what I am
after).
| | I am experimenting to run my own daemon on start up.
| ...
| However, if the program is interactive, I still can't imagine this to
| work, because there may be no terminals yet to read data from. To be
| more helpful, I need more details (if there are more questions).
| Perhaps putting the program startup command in the shell startup
| scripts would be more useful (without the '&' ,for example).
thanks again Bogdan:
here is more details:
#include "xyzCompany.h"
...
using namespace xyzCompany;
using namespace std;
int main()
{
xyzType myObj;
char* username=gary;
char* password=garyw;
while(1)
{
try {
myObj.login(username, password);
}
catch(myException f)
{
cout << "sample1: unable to connect, retying...." << endl;
continue;
}
break;
}
// wait for command line input
string c;
while( cin | c){ // cin is a standard input xterm prompt in
this case
if (c==end) myObj.logout();
if (c==dotask1) myObj.task1();
if (c==dotask2) myObj.task2();
...
}
}
say I lose connection with the server, then I need to re-connect
asap. cron job has a 1 minute minimum, that is too long, I need it
checked few times per second.
xyzType provide bool isLogged()
Read the following man pages: pthread_create, pthread_exit. It is
very simple to create a new thread, which would constantly check if
the program is connected. Something similar to:
#include <pthread.h
pthread_t threadID;
int main() {
...
pthread_create ( &threadID, NULL, &checkForConn, NULL );
// the '&' in front of 'checkForConn' may not be necessary.
...
while (1) {
if ( ! myObj.isLogged() ) {
// do reconnect here
}
//else pthread_yield(); // give away time for others
}
This should work well and is easy to learn.
If your while(cin| c) loop isn't too long and doesn't take long to
any of the functions called, you may check the status there, instead
of creating a new thread, like this:
while ( cin | c ) {
if ( ! myObj.isLogged() ) {
// do reconnect
}
if ( c == .....
...
}
If using the pthread library is impossible, read 'man 2 fork'
instead. Forking is a little bit less logical, as the old and the new
process both start/continue execution at the same place, right after
the fork() call. You need to check the result of fork() and branch
accordingly.
uname -r | grep -q "2.6.16"
if ( [ $? == 0 ]); then export ALSA_OUT_PORT=17:0
else export ALSA_OUT_PORT=65:0
fi;
The "$?" means the value returned by the previous command. Lookup
'man grep' for return values.